import 'package:flutter/material.dart';

///***********************************************
///文件描述：底部弹窗
///创建人：dgg108698
///创建时间：2020/9/1
///修改人：
///修改时间：
///修改备注：
///***********************************************

class CpsBottomDialog extends StatelessWidget {
  final Widget title;
  final Widget content;
  final Widget action;
  final EdgeInsetsGeometry contentPadding;

  CpsBottomDialog({
    this.title,
    @required this.content,
    this.action,
    EdgeInsetsGeometry contentPadding,
  }) : contentPadding =
            contentPadding ?? const EdgeInsets.symmetric(horizontal: 20.0);

  ///UI定义的表单类型底部弹窗
  factory CpsBottomDialog.form({
    Widget title,
    Widget positive,
    VoidCallback onTapPositive,
    Widget negative,
    VoidCallback onTapNegative,
    @required Widget content,
    EdgeInsetsGeometry contentPadding,
  }) {
    Widget titleBar;
    if (title != null || positive != null || negative != null)
      titleBar = _FromTitleBar(
        title: title,
        positive: positive,
        negative: negative,
        onTapNegative: onTapNegative,
        onTapPositive: onTapPositive,
      );
    return CpsBottomDialog(
      title: titleBar,
      content: content,
      contentPadding:
          contentPadding ?? EdgeInsets.only(left: 20.0, right: 20.0),
    );
  }

  factory CpsBottomDialog.feedback({
    @required String title,
    @required Widget content,
    String desc,
    VoidCallback onClose,
    Widget action,
    EdgeInsetsGeometry contentPadding,
  }) {
    return CpsBottomDialog(
      title: _FeedbackTitleBar(
        title: title,
        desc: desc,
        onClose: onClose,
      ),
      content: content,
      contentPadding:
          contentPadding ?? EdgeInsets.only(left: 20.0, right: 20.0, top: 16.0),
      action: action != null
          ? Container(
              height: 90.0,
              padding: EdgeInsets.all(20.0),
              child: action,
            )
          : null,
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget contentWidget = content;
    if (contentPadding != null)
      contentWidget = Padding(
        padding: contentPadding,
        child: contentWidget,
      );
    List<Widget> childList = <Widget>[
      if (title != null) title,
      if (content != null)
        Flexible(
          child: contentWidget,
        ),
      Container(
        constraints: BoxConstraints(
          minHeight: MediaQuery.of(context).viewInsets.bottom,
        ),
        alignment: Alignment.bottomCenter,
        child: action,
      ),
    ];

    return SafeArea(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: childList,
      ),
      top: false,
    );
  }
}

class _FromTitleBar extends StatelessWidget {
  final Widget title;
  final Widget positive;
  final Widget negative;
  final VoidCallback onTapPositive;
  final VoidCallback onTapNegative;

  _FromTitleBar({
    this.title,
    this.positive,
    this.negative,
    this.onTapPositive,
    this.onTapNegative,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      height: 68.0,
      padding: EdgeInsets.symmetric(horizontal: 20.0),
      child: Row(
        children: [
          GestureDetector(
            onTap: onTapNegative,
            behavior: HitTestBehavior.translucent,
            child: Container(
              height: 68.0,
              width: 64.0,
              alignment: Alignment.centerLeft,
              child: negative != null
                  ? DefaultTextStyle(
                      style: theme.appBarTheme?.textTheme?.headline6 ??
                          theme.textTheme.headline6,
                      child: negative,
                    )
                  : negative,
            ),
          ),
          Expanded(
            child: Center(
              child: DefaultTextStyle(
                style: theme.appBarTheme?.textTheme?.headline6 ??
                    theme.textTheme.headline6,
                child: title,
              ),
            ),
          ),
          GestureDetector(
            onTap: onTapPositive,
            behavior: HitTestBehavior.translucent,
            child: Container(
              height: 68.0,
              width: 64.0,
              alignment: Alignment.centerRight,
              child: DefaultTextStyle(
                style: theme.textTheme.button.apply(color: theme.primaryColor),
                child: positive,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _FeedbackTitleBar extends StatelessWidget {
  final VoidCallback onClose;
  final String title;
  final String desc;

  _FeedbackTitleBar({this.title, this.desc, this.onClose});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(left: 20.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (onClose != null)
            Align(
              alignment: Alignment.centerRight,
              child: GestureDetector(
                onTap: onClose,
                behavior: HitTestBehavior.opaque,
                child: Container(
                  padding: EdgeInsets.only(left: 20.0, top: 20.0, right: 20.0),
                  child: Icon(
                    Icons.close,
                    size: 12.0,
                  ),
                ),
              ),
            )
          else
            SizedBox(height: 20.0),
          if (title != null)
            Text(
              title,
              style: Theme.of(context)
                  .textTheme
                  .headline6
                  .merge(TextStyle(height: 1.0)),
            ),
          if (desc != null) SizedBox(height: 8.0),
          if (desc != null)
            Text(
              desc,
              style: Theme.of(context)
                  .textTheme
                  .bodyText2
                  .merge(TextStyle(height: 1)),
            ),
        ],
      ),
    );
  }
}

///限高为最高的0.8
Future<T> cpsShowBottomDialog<T>({
  @required BuildContext context,
  @required WidgetBuilder builder,
  Color backgroundColor,
  ShapeBorder shape,
  Clip clipBehavior,
  bool isDismissible,
  bool enableDrag,
}) {
  return showModalBottomSheet<T>(
    isScrollControlled: true,
    backgroundColor: backgroundColor,
    shape: shape,
    clipBehavior: clipBehavior,
    context: context,
    isDismissible: isDismissible ?? true,
    enableDrag: enableDrag ?? true,
    builder: (context) {
      return LayoutBuilder(
        builder: (context, constraints) {
          return ConstrainedBox(
            constraints: constraints.copyWith(
              maxHeight: constraints.maxHeight * 0.8,
            ),
            child: builder(context),
          );
        },
      );
    },
  );
}
